package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.GoodImg;
import com.bean.GoodMessage;
import com.bean.User;
import com.config.MinioAddr;
import com.mapper.GoodImgMapper;
import com.mapper.GoodMessageMapper;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bean.Good;
import com.mapper.GoodMapper;

@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService{

    @Autowired
    GoodMapper goodMapper;

    @Autowired
    GoodImgMapper goodImgMapper;

    @Autowired
    GoodMessageMapper goodMessageMapper;

    @Autowired
    UserMapper userMapper;


    // 根据用户id查询其拥有的宝贝
    @Override
    public List<Map<String, Object>> getGoodsByUserId(int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", id);
        List<Good> goods = goodMapper.selectByMap(map);

        List<Map<String, Object>> goodsList = new ArrayList<>();
        for (Good good : goods) {
            Map<String, Object> goodMap = new HashMap<>();
            // 根据宝贝id查询图片，仅要第一张
            goodMap.put("good_id", good.getId());
            goodMap.put("no", 1);

            Map<String, Object> newGoodMap = new HashMap<>();

            List<GoodImg> goodImgs = goodImgMapper.selectByMap(goodMap);
            if (goodImgs.size() != 0) {
                newGoodMap.put("img", MinioAddr.IMG_PATH + goodImgs.get(0).getSrc());
            }
            newGoodMap.put("id", good.getId());
            newGoodMap.put("description", good.getDescription());
            newGoodMap.put("price", good.getPrice());
            newGoodMap.put("soldOut", good.getSoldOut());
            newGoodMap.put("userId", good.getUserId());

            goodsList.add(newGoodMap);
        }

        return goodsList;
    }

    @Override
    public Map<String, Object> getPageOwnInfoByGoodId(int good_id) {
        Good good = goodMapper.selectById(good_id);
        User user = userMapper.selectById(good.getUserId());

        Map<String, Object> pageOwner = new HashMap<>();

        pageOwner.put("id", user.getId());
        pageOwner.put("nickName", user.getNickName());
        pageOwner.put("icon", MinioAddr.IMG_PATH + user.getIcon());
        pageOwner.put("sign", user.getSign());


        return pageOwner;
    }

    /**
     * 根据宝贝id查询其具体信息
     * @param good_id
     * @return
     */
    @Override
    public Map<String, Object> getGoodInfoByGoodId(int good_id) {
        // 将要返回的map
        // 其中包含 宝贝详情 宝贝的图片 宝贝的评论
        Map<String, Object> goodMap = new HashMap<>();

        // ********************************************
        // 1、获取宝贝信息
        Good good = goodMapper.selectById(good_id);
        goodMap.put("good", good);

        // ********************************************
        // 2、获取宝贝图片
        Map<String, Object> map = new HashMap<>();
        map.put("good_id", good_id);
        List<GoodImg> goodImgs = goodImgMapper.selectByMap(map);

        List<Map<String, Object>> goodImgsList = new ArrayList<>();
        for (GoodImg goodImg : goodImgs) {
            // 仅获取 no 和 src 即可
            Map<String, Object> goodImgMap = new HashMap<>();
            goodImgMap.put("no", goodImg.getNo());
            goodImgMap.put("src", MinioAddr.IMG_PATH + goodImg.getSrc());
            goodImgsList.add(goodImgMap);
        }
        goodMap.put("goodImg", goodImgsList);

        // ********************************************
        // 3、获取宝贝评论 条件同上map
        List<GoodMessage> goodMessages = goodMessageMapper.selectByMap(map);

        // 将获取的宝贝评论按要返回的内容封装成一个新的list
        List<Map<String, Object>> goodMessagesList = new ArrayList<>();
        for (GoodMessage goodMessage : goodMessages) {
            // 填充宝贝信息
            Map<String, Object> goodMessageMap = new HashMap<>();
            goodMessageMap.put("no", goodMessage.getNo());
            goodMessageMap.put("fatherNo", goodMessage.getFatherNo());
            goodMessageMap.put("userId", goodMessage.getUserId());
            goodMessageMap.put("date", goodMessage.getDate());
            goodMessageMap.put("text", goodMessage.getText());
            goodMessageMap.put("sendType", goodMessage.getSendType());
            goodMessageMap.put("sendToUserId", goodMessage.getSendToUserId());
            goodMessageMap.put("sendToUserNickName", goodMessage.getSendToUserNickName());
            goodMessageMap.put("messageChecked", goodMessage.getMessageChecked());

            // 获取用户信息
            User user = userMapper.selectById(goodMessage.getUserId());
            goodMessageMap.put("icon", MinioAddr.IMG_PATH + user.getIcon());
            goodMessageMap.put("nickName", user.getNickName());

            goodMessagesList.add(goodMessageMap);
        }

        // 将留言按其父子关系封装成新的list
        List<Map<String, Object>> goodMessagesList2 = new ArrayList<>();
        for (Map<String, Object> goodMessage : goodMessagesList) {
            // 判断是否为父留言
            if ((int)goodMessage.get("fatherNo") == 0) {
                // 遍历留言list 如果留言的fatherNo 和 父留言的no相同则添加进sonMessages
                List<Map<String, Object>> sonMessages = new ArrayList<>();
                for (int i = 0; i < goodMessagesList.size(); i++) {
                    if (goodMessagesList.get(i).get("fatherNo") == goodMessage.get("no")) {
                        sonMessages.add(goodMessagesList.get(i));
                    }
                }

                goodMessage.put("sonMessages", sonMessages);

                goodMessagesList2.add(goodMessage);
            }

        }
        goodMap.put("goodMessages", goodMessagesList2);

        return goodMap;
    }

    /**
     * 根据宝贝id获取宝贝页面留言数
     * @param goodId
     * @return
     */
    @Override
    public int countGoodMessagesByGoodId(int goodId) {
        Map<String, Object> map = new HashMap<>();
        map.put("good_id", goodId);
        List<GoodMessage> goodMessages = goodMessageMapper.selectByMap(map);

        return goodMessages.size();
    }

    /**
     * 插入新留言
     * @param message
     * @return
     */
    @Override
    public int addGoodMessage(GoodMessage message) {
        return goodMessageMapper.insert(message);
    }
    @Override
    public int insertGood(Good good) {
        return goodMapper.insert(good);
    }
    @Override
    public List<Good> getGoodsOnlyInfoByUserId(int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", id);
        List<Good> goods = goodMapper.selectByMap(map);

        return goods;
    }
/**
     *主页推荐商品
     * 默认一页8个商品
     * @return
     */
    @Override
    public List<Map<String, Object>> seletgoods(int page){
        List<Map<String, Object>> goodsList = new ArrayList<>();
        Page<Good> page1 = new Page<>(page,8);
        QueryWrapper<Good> wrapper = new QueryWrapper<>();
        Page<Good> goodPage = goodMapper.selectPage(page1, wrapper);
        List<Good> records = goodPage.getRecords();
        for (Good record : records) {
            Map<String, Object> goodMap = new HashMap<>();
           //查第一张图当首页显示
            Map<String, Object> map = new HashMap<>();
            map.put("good_id",record.getId());
            List<GoodImg> goodImgs = goodImgMapper.selectByMap(map);
            goodMap.put("src",goodImgs.get(0).getSrc());
            //查id等
            goodMap.put("id",record.getId());
            goodMap.put("user_id",record.getUserId());
            goodMap.put("description",record.getDescription());
            goodMap.put("price",record.getPrice());
            //查头像和名字
            User user = userMapper.selectById(record.getUserId());
            goodMap.put("icon",user.getIcon());
            goodMap.put("nickName", user.getNickName());

     goodsList.add(goodMap);
        }
        return goodsList;
    }

    /**
     *  模糊搜索，用户名
     * @param page
     * @param text
     * @return
     */
    @Override
    public List<Map<String, Object>> Search(int page, String text) {
        List<Map<String, Object>> SearchList = new ArrayList<>();
        Page<Good> page1 = new Page<>(page,8);
        //模糊查商品名字
        QueryWrapper<Good> wrapper1 = new QueryWrapper<>();
        wrapper1.like("description",text);
        Page<Good> goodPage = goodMapper.selectPage(page1,wrapper1);
        if(goodPage!=null){
            List<Good> records = goodPage.getRecords();
            for (Good record : records) {
                Map<String, Object> goodMap = new HashMap<>();
                //查第一张图当首页显示
                Map<String, Object> map = new HashMap<>();
                map.put("good_id",record.getId());
                List<GoodImg> goodImgs = goodImgMapper.selectByMap(map);
                goodMap.put("src",goodImgs.get(0).getSrc());
                //查id等
                goodMap.put("id",record.getId());
                goodMap.put("user_id",record.getUserId());
                goodMap.put("description",record.getDescription());
                goodMap.put("price",record.getPrice());
                //查头像和名字
                User user = userMapper.selectById(record.getUserId());
                goodMap.put("icon",user.getIcon());
                goodMap.put("nickName", user.getNickName());

                SearchList.add(goodMap);
            }
        }
        return SearchList;
    }

    /**
     * 此方法用于获取要联系的对象信息以及商品的简略信息
     *
     * @return
     */
    @Override
    public Map<String, Object> lianXiTa(int userId, int goodId) {
        User user = userMapper.selectById(userId);
        Good good = goodMapper.selectById(goodId);
        Map<String, Object> map = new HashMap<>();
        map.put("good_id", goodId);
        List<GoodImg> goodImgs = goodImgMapper.selectByMap(map);

        Map<String, Object> userMap = new HashMap<>();
        userMap.put("id", user.getId());
        userMap.put("icon", MinioAddr.IMG_PATH + user.getIcon());
        userMap.put("nickName", user.getNickName());

        Map<String, Object> goodMap = new HashMap<>();
        goodMap.put("id", good.getId());
        goodMap.put("address", good.getAddress());
        goodMap.put("price", good.getPrice());
        goodMap.put("img_src", MinioAddr.IMG_PATH + goodImgs.get(0).getSrc());

        Map<String, Object> outputMap = new HashMap<>();
        outputMap.put("TA", userMap);
        outputMap.put("good", goodMap);

        return outputMap;
    }

}
